<?php
namespace app\index\controller;

class Member extends Common
{   
    //用户详情
    public function member_info(){
        $member = db('member')->where(['id'=>$this->info['id']])->find();

        if(request()->isPost()){
            $passwd = input('passwd');
            $confirm_password = input('confirm_password');
            $company = input('company');
            $contact = input('contact');
            $user_industry = input('user_industry');
            $users_location = input('users_location');
            $telephone = input('telephone');
            $qq = input('qq');
            $email = input('email');
            $province = input('province');    
            $city = input('city');  
            $area = input('area');

            if(!empty($passwd)){
                $data['password'] = md5($passwd);
            }

            if($confirm_password != $passwd)$this->error('两次密码不正确!');

            $data['company'] = $company;
            $data['contact'] = $contact;
            $data['user_industry'] = $user_industry;
            $data['users_location'] = $users_location;
            $data['telephone'] = $telephone;
            $data['qq'] = $qq;
            $data['email'] = $email;
            $data['province'] = $province;
            $data['city'] = $city;
            $data['area'] = $area;

            db('member')->where(['id'=>$this->info['id']])->update($data);

            $this->success("修改成功！");
        }   

        $this->assign('member',$member);
        return $this->fetch();
    }

	//我的消费统计
    public function consumption_record()
    {  
        $type=input('type');  	
        $where['uid'] = $this->info['id'];
        if(!empty($type)){
            $where['type'] = $type;
        }

        $list = db("member_flow")
            ->where($where)
            ->order("id desc")
            ->paginate(10)->each(function($item){
                $item['time'] = date('Y-m-d h:i:s',$item['time']);

                if($item['text_content'] != '充值'){
                    $text_content = json_decode($item['text_content'],1);
                    
                    $item['moble_count'] = isset($text_content['moble_count'])?$text_content['moble_count']:'';
                    $item['sms_price'] = isset($text_content['sms_price'])?$text_content['sms_price']:'';

                }else{
                    $item['text_content'] = '';
                }
                return $item;
        });


        $list_count = db('member_flow')->where($where)->count('id');

        //充值总额
        $total_money = db('member_flow')->where(['uid'=>$this->info['id'],'type'=>'1'])->sum('money');
        $cat_money = db('member_flow')->where(['uid'=>$this->info['id'],'type'=>'-1'])->sum('money');
        
        $this->assign('type',$type);
        $this->assign('total_money',$total_money);
        $this->assign('cat_money',$cat_money);
        $this->assign('list', $list);
        $this->assign('list_count', $list_count);
        return $this->fetch();
    }

    //我的通讯录
    public function address_book(){
        $keyword = input('keyword');
        $where['uid'] = $this->info['id'];
        $whereor = [];

        if(!empty($keyword)){
            $where['name'] = ['like',"%{$keyword}%"];
        }

        $list = db("address_book")
            ->where($where)
            ->order("id desc")
            ->paginate(10);
        $list_count = db('address_book')->where($where)->count('id');

        $this->assign('list', $list);
        $this->assign('list_count', $list_count);
        $this->assign('keyword', $keyword);
        return $this->fetch();
    }

    //新增通讯录
    public function address_book_add(){
        $id = input('id');

        if(request()->isPost()){
            $name = input('name');
            $mobile = input('mobile');

            if(empty($name))$this->error('通讯录名称为空！');
            if(!preg_match("/^1[345678]{1}\d{9}$/", $mobile))$this->error('电话格式不对！');

            $data['name'] = $name;
            $data['mobile'] = $mobile;

            if(empty($id)){
                $data['uid'] = $this->info['id'];
                db('address_book')->insert($data);
            }else{
                db('address_book')->where(['id'=>$id])->update($data);
            }

            $this->redirect("Member/address_book");
        }

        $address_book = db('address_book')->where(['id'=>$id])->find();
        $this->assign('id',$id);
        $this->assign('address_book',$address_book);

        return $this->fetch();
    }

    //删除通讯录
    public function address_book_del(){
        $id = input('id');

        $address_book = db('address_book')->where(['id'=>$id])->find();
        if($address_book['uid'] != $this->info['id'])$this->error('你无权删除别人通讯录!');

        db('address_book')->where(['id'=>$id])->delete();
    }

    //合同显示
    public function contract_display(){
        $id = input('id');
        
        $set_value = db('set')->where(['id'=>$id])->value('value');

        $this->assign('set_value',$set_value);
        return $this->fetch();
    }

    //注意事项
    public function contract_display_sms(){
        $id = input('id');
        
        $set_value = db('set')->where(['id'=>$id])->value('value');

        $this->assign('set_value',$set_value);
        return $this->fetch();
    }
}
